

#include "save.h"
#include "config.h"
#include "ziku.h"
unsigned char Read_PRJ(unsigned char p,unsigned char *Prj_data)
{
unsigned char i,sum,*point;
unsigned int idata address;
if(p>=5)
   {p-=5;
    address=PRJ_ADDR+p*128;
    read_page(Prj_data,0x53,address,90);
    sum=*Prj_data;
    for(i=1;i<90;i++) sum+=*(Prj_data+i);
    if(sum==*(Prj_data+90)) return 0;
    return 255;
    }
point=&Prj_bak[p].Curv_count;
for(i=0;i<91;i++) *(Prj_data+i)=*(point+i);
return 0;
}

void Save_Prj(unsigned char id,unsigned char *Prj_data)
{
unsigned char i,sum;
unsigned int idata address;

if(id<5) return;
id-=5;
address=PRJ_ADDR+id*128;
sum=*Prj_data;
for(i=1;i<90;i++) sum+=*(Prj_data+i);
write_page(Prj_data,0x53,address,90);
write_byte(sum,0x53,address+90);
}

void Read_Prjcnt()
{
unsigned char sum,car;
sum=read_byte(0x53,I2C_PRJAMOUNT);
sum+=read_byte(0x53,I2C_PRJAMOUNT+1);
car=read_byte(0x53,I2C_PRJAMOUNT+2);
if(car!=sum) MAXPRJ_CNT=5;
else MAXPRJ_CNT=read_byte(0x53,I2C_PRJAMOUNT)+5;
}

unsigned char Read_NCCode()
{
return(Read_bytesum(I2CNC_CODE));
}

unsigned char Read_JiaChun()
{
return(Read_bytesum(I2CJIA_CHUN));
}

unsigned char Read_Dianfen()
{
return(Read_bytesum(I2CDIANFEN_MEI));
}

unsigned char Read_bytesum(unsigned int addr)
{
unsigned char sum,car;
sum=read_byte(0x53,addr);
sum+=read_byte(0x53,addr+1);
car=read_byte(0x53,addr+2);
if(car!=sum) 
return 255;
else
return(read_byte(0x53,addr));
}


unsigned char Read_Yousuan()
{
return(Read_bytesum(I2CYOUSUAN_JIA));
}


unsigned char Read_Cuzongsuan()
{
return(Read_bytesum(I2CCU_ZONGSUAN));
}


unsigned char Read_Jianyousuan()
{
return(Read_bytesum(I2CJIANGYOU_SUAN));
}


unsigned char Read_Fengmisuan()
{
return(Read_bytesum(I2CFENGMI_SUAN));
}

unsigned char Read_MAXWAVECNT()
{
unsigned char car;
car=Read_bytesum(I2C_WAVEAMOUNT);
if((car>14)||(car==0)) car=1;
return(car);
}

void Read_Wave(unsigned char idata p,unsigned char idata *WN)
{
unsigned char idata i,car,flag=0;
unsigned int addr;

addr=I2C_WAVELENGTH+3*p;
for(i=0;i<3;i++)
   {car=read_byte(0x53,addr+i);if(car>9) flag=1;
    *(WN+i)=car;}
if(flag==1)
   {for(i=0;i<3;i++) *(WN+i)=0;}
}

//If there is no result has been saved in this ID_room,return flag=255;
unsigned char Read_result(unsigned int id,unsigned char *flag)
{
unsigned char  idata i,sum=0;
unsigned char *p;
unsigned int idata address;

address=RESULT_ADDR+id*0x10;
p=&Test_data.flag;
read_page(p,0x53,address,14);
for(i=0;i<14;i++) sum+=*(p+i);
if(Test_data.sum==sum) (*flag)=0;
else (*flag)=255;
return sum;
}

void Read_HZindex()
{
unsigned char sum;
unsigned int len,p;
struct H_uint idata tmp;

tmp.val.b[1]=read_byte(0x53,HANZIKU_NUM);
tmp.val.b[0]=read_byte(0x53,HANZIKU_NUM+1);
sum=read_byte(0x53,HANZIKU_NUM+2);
if(sum!=(tmp.val.b[0]+tmp.val.b[1]))
    tmp.val.a=1;
p=HANZIKU_INDEX;
for(len=0;len<tmp.val.a;len++)
   {HZ_Index[len][0]=read_byte(0x53,p);p++;
    HZ_Index[len][1]=read_byte(0x53,p);p++;
    }
HZ_Index[len][0]='\0';
}
/*
//If there is no result has been saved in this ID_room,return 255;
unsigned char Read_result(unsigned int id,unsigned char *flag)
{
struct H_float idata f;
unsigned char  idata i,car,sum=0;
unsigned int idata address;

address=RESULT_ADDR+id*0x10;
Test_data.flag=read_byte(0x53,address);address++;sum=sum+Test_data.flag;
Test_data.Year=read_byte(0x53,address);address++;sum=sum+Test_data.Year;
Test_data.Mon=read_byte(0x53,address);address++;sum=sum+Test_data.Mon;
Test_data.Day=read_byte(0x53,address);address++;sum=sum+Test_data.Day;
Test_data.Hour=read_byte(0x53,address);address++;sum=sum+Test_data.Hour;
Test_data.Min=read_byte(0x53,address);address++;sum=sum+Test_data.Min;
Test_data.Second=read_byte(0x53,address);address++;sum=sum+Test_data.Second;
Test_data.Operator=read_byte(0x53,address);address++;sum=sum+Test_data.Operator;
Test_data.Prj_code=read_byte(0x53,address);address++;sum=sum+Test_data.Prj_code;
Test_data.Wave_code=read_byte(0x53,address);address++;sum=sum+Test_data.Wave_code;
for(i=0;i<4;i++)
	{car=read_byte(0x53,address);address++;sum=sum+car;f.val.b[i]=car;}
Test_data.result=f.val.a;
Test_data.sum=read_byte(0x53,address);
if(Test_data.sum==sum) (*flag)=0;
else (*flag)=255;
return sum;
}
*/
